//
//  SettingVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/5/30.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

class SettingVC: BaseTabVC {
    
    @IBOutlet weak var cacheSizeLabel: UILabel!
    @IBOutlet weak var userLocationSwitch: UISwitch!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "设置"
        self.reloadTB()
    }
    
    private func reloadTB() {
        
        
        UIImageView.getImageCacheSize(completion: {
            [weak self] size in
            guard let `self` = self else { return }
            
            self.cacheSizeLabel.text = self.changeSizeToString(fileSize: Float(size))
        })
        
        self.userLocationSwitch.isOn = (AppManager.share.user?.status ?? 0) == 11 ? false : true;
    }
    
    private func changeSizeToString(fileSize:Float) -> String? {
        
        
        var sizeText = "\(fileSize)B";
        
        if (fileSize >= pow(10, 9)) { // size >= 1GB
            
            sizeText = String(format: "%.2fGB", fileSize / pow(10, 9))
        } else if (fileSize >= pow(10, 6)) { // 1GB > size >= fileSize
            
            sizeText = String(format: "%.2fMB", fileSize / pow(10, 6));
        } else if (fileSize >= pow(10, 3)) { // 1MB > size >= 1KB
            sizeText = String(format: "%.2fKB", fileSize / pow(10, 3));
        }
        
        return sizeText;
    }
    
    
    @IBAction func chanStatusAction(_ sender: Any) {
        
        NetAPI.userhide.request {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            guard let res = response as? [String:Any?] else {
                
                self.showToast(errmsg)
                return
            }
            
            if let status = res["status"] as? Int {
                
                AppManager.share.user?.status = status
                self.reloadTB()
            }
        }
    }
}

extension SettingVC {
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.section == 1 { // 退出
            
            let alertVC = UIAlertController(title: "", message: "确认退出登录", preferredStyle: UIAlertControllerStyle.alert)
            
            let okAction = UIAlertAction(title: "确认", style: UIAlertActionStyle.default) {
                [weak self] _ in
                guard let `self` = self else { return }
                
                AppManager.share.user = nil
                AppManager.share.token = nil
                AppManager.share.code = nil
                self.navigationController?.popViewController(animated: true)
            }
            
            alertVC.addAction(okAction)
            
            alertVC.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil))
            
            self.present(alertVC, animated: true, completion: nil)
            
            return
        }
        
        if indexPath.row == 1 {
            
            let alertVC = UIAlertController(title: "", message: "清除缓存", preferredStyle: UIAlertControllerStyle.alert)
            
            let okAction = UIAlertAction(title: "确认", style: UIAlertActionStyle.default) {
                [weak self] _ in
                guard let `self` = self else { return }
                
                UIImageView.clearCache()
                self.reloadTB()
            }
            
            alertVC.addAction(okAction)
            
            alertVC.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil))
            
            self.present(alertVC, animated: true, completion: nil)
        }
        
    }
}
